package apidayfile;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Scanner;

public class FileDemo {
    public static void main(String[] args) throws IOException {
        //test01
        for (int i = 1; i <= 100 ; i++) {
            File file = new File("test"+i+".txt");
            if (file.isFile()){
                System.out.println("该文件已经存在");
            }else {
                file.createNewFile();
            }
        }

        //test02
        for (int i = 1; i <= 100; i++) {
            File file = new File("test"+i+".txt");
            if (file.exists()){
                file.delete();
            }
        }

        //test03
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你想要创建的目录名");
        String input = scan.next();
        File dir = new File("./"+input);
        if(dir.isDirectory()){
            System.out.println("该目录已经存在");
        }else {
            dir.mkdirs();
            System.out.println("该目录已经创建");
        }


        //test04
        System.out.println("请输入你想要删除的目录名");
        input = scan.next();
        File dir2 = new File("./"+input);
        if(dir2.exists()){
            dir2.delete();
        }else {
            System.out.println("该目录已经不存在");
        }

        //test05
        System.out.println("请输入你想要检索并创建的文件名");
        input = scan.next();
        File file = new File("./"+input);
        while (true){
            if (file.exists()){
                System.out.println("此文件已存在，请重新输入你想要检索并创建的文件名");
                input = scan.next();
                file = new File("./"+input);
            }else {
                file.createNewFile();
                break;
            }
        }

        //test06
        File dir3 = new File(".");
        File [] sub = dir3.listFiles(pathname -> pathname.getName().contains("s"));
        for (File s:sub
             ) {
            System.out.println("Lambda表达写法下所有包含s的子集的名字："+s.getName());
        }




        File [] sub2 = dir3.listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                return pathname.getName().contains("s");
            }
        });
        for (File f: sub2){
            System.out.println("匿名内部类写法下所有包含s的子集的名字："+f.getName());
        }











    }
}
